﻿// <copyright file="_PACKAGE_FILE_GUID_INFO.cs" company="Sinclair Community College">
// Copyright 2010 Sinclair Community College
// </copyright>
// <originalAuthor>Patrick S. Seymour (patrick.seymour@gmail.com)</originalAuthor>

namespace SinclairCC.AppV
{
    using System;

    public class _PACKAGE_FILE_GUID_INFO
    {
        /// <summary>
        /// A unique identifier of the file/directory that belongs to the package.
        /// </summary>
        private Guid fileGuid;

        /// <summary>
        /// Index of the corresponding container of the package file/directory.
        /// </summary>
        private uint index;

        /// <summary>
        /// Initializes a new instance of the _PACKAGE_FILE_GUID_INFO class.
        /// </summary>
        public _PACKAGE_FILE_GUID_INFO()
        {
            this.fileGuid = Guid.Empty;
            this.index = 0;
        }

        public _PACKAGE_FILE_GUID_INFO(System.IO.BinaryReader binaryStreamReader)
            : this()
        {
            this.ReadContainer(binaryStreamReader);
        }

        /// <summary>
        /// Reads the actual data for this container from the file.
        /// </summary>
        protected void ReadContainer(System.IO.BinaryReader streamReader)
        {
            if (streamReader.BaseStream.Position <= (streamReader.BaseStream.Length - 16))
            {
                byte[] guidBytes = streamReader.ReadBytes(16);
                this.fileGuid = new Guid(guidBytes);
            }
            else
            {
                streamReader.BaseStream.Seek(0, System.IO.SeekOrigin.End);
            }

            if (streamReader.BaseStream.Position <= (streamReader.BaseStream.Length - sizeof(uint)))
            {
                this.index = streamReader.ReadUInt32();
            }
            else
            {
                streamReader.BaseStream.Seek(0, System.IO.SeekOrigin.End);
            }
        }

        public Guid FileGuid
        {
            get { return this.fileGuid; }
        }

        public uint Index
        {
            get { return this.index; }
        }

        public override string ToString()
        {
            return string.Format("_PACKAGE_FILE_GUID_INFO: Index = {0}, GUID = {1}", this.index, this.fileGuid);
        }
    }
}
